package com.gxks.lhs.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Goods;
import com.gxks.lhs.entity.Report;
import com.gxks.lhs.security.SecurityUtils;
import com.gxks.lhs.service.IGoodsService;
import com.gxks.lhs.service.IReportService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 举报表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/restful-api/report")
public class UserReportController {

    @Autowired
    private IReportService reportService;
    @Autowired
    private IGoodsService goodsService;


    @ApiOperation("根据举报者id获取举报列表")
    @GetMapping("reportList")
    public R reportList(@RequestParam Integer reporterId,
                        @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                        @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Report> reportList = reportService.findReportByReporterId(reporterId, current, size);
        if (reportList != null) {
            return R.success(reportList).setMsg("查询成功");
        }
        return R.error("查询失败");
    }


    @ApiOperation("新增举报")
    @PostMapping("addReport")
    public R addReport(@RequestBody Report report) {
        if (report == null) {
            return R.error().setMsg("举报信息不能为空");
        }
        String userId = SecurityUtils.getCurrentUserId();
        report.setUserId(userId);
        report.setStatus(0);
        Goods goods = goodsService.findGoodsById(report.getGoodsId());
        if (goods == null) {
            return R.error().setMsg("商品不存在");
        }
        report.setGoodsName(goods.getTitle());
        report.setId("R" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + Math.round(Math.random() * 100000));
        if (reportService.addReport(report)) {
            return R.success().setMsg("举报成功，等待管理员处理");
        }
        return R.error().setMsg("发生了一些错误，请稍后再试吧");
    }

    @ApiOperation("根据id删除举报")
    @DeleteMapping("deleteReport")
    public R deleteReport(@RequestParam Integer reportId) {
        if (reportService.deleteReport(reportId)) {
            return R.success().setMsg("删除成功");
        }
        return R.error().setMsg("删除失败");
    }

    @ApiOperation("修改举报信息")
    @PutMapping("updateReport")
    public R updateReport(@RequestBody Report report) {
        if (reportService.updateReport(report)) {
            return R.success().setMsg("修改成功");
        }
        return R.error("修改失败");
    }

}

